#include <unistd.h>
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>

#define CONTENT "This is share memory!"

int main() {
  int id = shmget(0x8888, getpagesize(), IPC_CREAT | IPC_EXCL | 0664);
  printf("id = %d\n", id); 
  if (id < 0) {
    perror("shmget");
    return -1;
  }

  char *buf = shmat(id, NULL, 0);
  printf("buf = %p\n", buf);
  if (buf == (char*)-1) {
    perror("shmat");
    return -1;
  }

  printf("write content: %s\n", CONTENT);
  strcpy(buf, CONTENT);

  if (shmdt(buf) < 0) {
    perror("shmdt");
    return -1;
  }
  
  return 0;
}
